/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mmt_client;

/**
 *
 * @author CUCAINHO
 */


import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;
import Message.*;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * This class is responisble for sending sreenshot every predefined duration
 */
class ScreenSpyer implements Runnable   {

    Socket socket = null; 
    Robot robot = null; // Used to capture screen
    Rectangle rectangle = null; //Used to represent screen dimensions
    boolean continueLoop = true; //Used to exit the program
    
   /* public ReciveServer (Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} */
    
    public ScreenSpyer(Socket socket, Robot robot,Rectangle rect) {
        this.socket = socket;
        this.robot = robot;
        this.rectangle = rect;
        Thread t = new Thread(this);
        t.start();
    }

    public  void run(){
        
        ObjectOutputStream oos = null; //Used to write an object to the streem       
       
        try{
          //Prepare ObjectOutputStream
          oos = new ObjectOutputStream(socket.getOutputStream());
          System.out.println("Send rec ịn client!!!!!!!!!!!!!!!1==============================================================start");
          /*
           * Send screen size to the server in order to calculate correct mouse
           * location on the server's panel
           */
          //Object obj = new String("Send rec ịn client!!!!!!!!!!!!!!!1==============================================================");
          
         // Message msrec = new Message(5,1, obj);
          Message msrec = new Message(5,3, rectangle);
          oos.writeObject(msrec);
          System.out.println("Send rec ịn client!!!!!!!!!!!!!!!1==============================================================finsnich");
         }catch(IOException ex){
            ex.printStackTrace();
         }
        
       

        ///
          
          
          
       while(continueLoop){
            //Capture screen
           System.out.println("zo ham send imageaa!");
           
              try{
            //Prepare ObjectOutputStream
            oos = new ObjectOutputStream(socket.getOutputStream());
            /*
             * Send screen size to the server in order to calculate correct mouse
             * location on the server's panel
             */
           // oos.writeObject(rectangle);
            }catch(IOException ex){
                ex.printStackTrace();
            }

             
             
            BufferedImage image = robot.createScreenCapture(rectangle);
            /* I have to wrap BufferedImage with ImageIcon because BufferedImage class
             * does not implement Serializable interface
             */
            ImageIcon imageIcon = new ImageIcon(image);
            

            //Send captured screen to the server
            try {
                System.out.println("before sending image");
                
                Object obj = new String("Congtt");
                Message ms = new Message(5, 2 , imageIcon);
               // Message ms = new Message(5, 2 , obj);
                
                oos.writeObject(ms);
                
                //oos.writeObject(imageIcon);
                
              // oos.reset(); //Clear ObjectOutputStream cache
                System.out.println("New screenshot sent");
            } catch (IOException ex) {
               ex.printStackTrace();
            }

            //wait for 100ms to reduce network traffic
           /* try{
                Thread.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }*/
        }

    }

}

